<?php
namespace app\manage\controller;
use think\facade\Db;
use app\manage\controller\Base;
use app\manage\validate\GoodsCateValidate;
use app\model\GoodsCateModel;
use support\Request;
use support\View;

class GoodsCateController extends Base
{

    public function index(Request $request)
    {
		$key=$request->get('key','');
		View::assign('key', $key);
		$datasort=GoodsCateModel::where('name','like','%'.$key.'%')->order('sort asc')->select();
		View::assign('datasort', $datasort);
		$count1=sizeof($datasort);
		View::assign('count1', $count1);
        return view('goods_cate/index');
    }
	public function ajax(Request $request)
    {
    	$data=$request->all();
		if($data['type']=='goods_cate_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$goods_cate=new GoodsCateModel();
			$info=$goods_cate->batchUpdate($ar);
			if($info){
				return json(['code'=>1]);
			}else{
				return json(['code'=>0]);
			}
		}
		if($data['type']=='goods_cate_del'){
            if(GoodsCateModel::destroy($data['id'])){
                return json(['code'=>1]);
            }else{
                return json(['code'=>0]);
            }
		}
		if($data['type']=='goods_cate_start'){
			if(GoodsCateModel::where('id',$data['id'])->update(['isopen'=>1])){
				return json(['code'=>1]);
			}else{
				return json(['code'=>0]);
			}
		}
		if($data['type']=='goods_cate_stop'){
			if(GoodsCateModel::where('id',$data['id'])->update(['isopen'=>0])){
				return json(['code'=>1]);
			}else{
				return json(['code'=>0]);
			}
		}
		return json(['code'=>0]);
    }
	public function add(Request $request)
    {
    	if(request()->method()=='POST'){
    		$data=$request->post();
			
			$validate = new GoodsCateValidate();
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}	

			$file = $request->file('pic');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/goods/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
            }


			$user = new GoodsCatemodel($data);
			$res=$user->save();
			if($res){
				return view_s('添加成功',url('goods_cate/index',['st'=>1]));
			}else{
				return view_e('栏目添加失败了');
			}
    	}
		
	
		return view('goods_cate/add');
    }
	public function edit(Request $request)
    {
    	if(request()->method()=='POST'){
    		$data=$request->post();
			$validate = new GoodsCateValidate();
			if(!$validate->scene('edit')->check($data)){
				return view_e($validate->getError());
			}	

			
			$file = $request->file('pic');
            if ($file && $file->isValid() && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/goods/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
            }
    		$user = new GoodsCatemodel();
			$res=$user->save($data,['id' => input('id')]);
			if($res){
				return view_s('修改成功',url('goods_cate/index',['st'=>1]));
			}else{
				return view_e('栏目修改失败了');
			}
    	}
		
		$cid=$request->get('id',0);
		$data=Db::name('goods_cate')->where('id',$cid)->find();
		View::assign('data',$data);
		return view('goods_cate/edit');
    }
}
